<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/2
 * Time: 17:24
 */

namespace App\Admin\Models;

/**
 * App\Admin\Models\EmployeeModel
 *
 * @property int                              $employee_id           员工ID
 * @property int                              $user_id               用户ID，admin_users表主键
 * @property int                              $sequence_pid          序列ID，admin_sequences表主键
 * @property int                              $sequence_id           序列ID，admin_sequences表主键
 * @property int                              $position_id           职位ID，admin_positions表主键
 * @property int                              $order                 排序
 * @property string                           $avatar                图像地址
 * @property string                           $email                 邮箱地址
 * @property string                           $phone                 办公电话
 * @property string                           $name_zh               姓名-中文
 * @property string                           $name_en               姓名-英文
 * @property string                           $office_address_zh     办公室地址-中文
 * @property string                           $office_address_en     办公室地址-英文
 * @property string|null                      $research_direction_zh 研究方向-中文
 * @property string|null                      $research_direction_en 研究方向-英文
 * @property string|null                      $personal_profile_zh   个人简介-中文
 * @property string|null                      $personal_profile_en   个人简介-英文
 * @property string                           $personal_homepage     个人主页地址
 * @property \Illuminate\Support\Carbon       $created_at            创建时间
 * @property \Illuminate\Support\Carbon       $updated_at            最后更新时间
 * @property-read \App\Admin\Models\UserModel $user
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereEmployeeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereNameEn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereNameZh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereOfficeAddressEn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereOfficeAddressZh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel wherePersonalHomepage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel wherePersonalProfileEn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel wherePersonalProfileZh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel wherePositionId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereResearchDirectionEn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereResearchDirectionZh($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereSequenceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereSequencePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|EmployeeModel whereUserId($value)
 * @mixin \Eloquent
 */
class EmployeeModel extends BaseModel
{
    protected $table = 'admin_employees';

    protected $primaryKey = 'employee_id';

    protected $fillable = [
        'user_id',
        'sequence_pid',
        'sequence_id',
        'position_id',
        'order',
        'avatar',
        'email',
        'phone',
        'name_zh',
        'name_en',
        'office_address_zh',
        'office_address_en',
        'research_direction_zh',
        'research_direction_en',
        'personal_profile_zh',
        'personal_profile_en',
        'personal_homepage',
        'engineering',
        'graduation_destination'
    ];

    protected $guarded = [];

    public function getSequencePidAttribute($value)
    {
        return explode(',', $value);
    }

    public function setSequencePidAttribute($value)
    {
        $this->attributes['sequence_pid'] = implode(',', $value);
    }

    public function getSequenceIdAttribute($value)
    {
        return explode(',', $value);
    }

    public function setSequenceIdAttribute($value)
    {
        $this->attributes['sequence_id'] = implode(',', $value);
    }

    /**
     * 查教师列表
     *
     * @param array $params
     * @param int   $contentId
     *
     * @return array|\Illuminate\Support\Collection
     */
    public static function getTeacherList(array $params = [], int $contentId = 0)
    {
        $employeeIds = [];
        $list        = static::select(['employee_id', 'name_zh', 'name_en', 'avatar']);
        if (!empty($params['employee_ids'])) {
            $list = $list->whereIn('employee_id', $params['employee_ids'])
                ->orderBy(\DB::raw('FIELD(employee_id,' . implode(',', $params['employee_ids']) . ')'));
        } elseif (!empty($contentId)) {
            $content = ContentModel::find($contentId);
            if (!empty($content['employee_ids'])) {
                $employeeIds = $content['employee_ids'];
            }
        }
        $list = $list->get();

        if (!empty($employeeIds) && !empty($list)) {
            // 重新排序
            $hasSelected = [];
            $list = array_column(json_decode(json_encode($list), true), null, 'employee_id');
            foreach ($employeeIds as $employeeId) {
                if (isset($list[$employeeId])) {
                    $hasSelected[] = $list[$employeeId];
                    unset($list[$employeeId]);
                }
            }
            $list = array_merge(array_values($list), $hasSelected);
        }

        return !empty($list) ? json_decode(json_encode($list), true) : [];
    }

    /**
     * 和UserModel一对一的关系
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(UserModel::class, 'user_id');
    }

    /**
     * 和EmployeeDetailModel一对一的关系
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function detail()
    {
        return $this->belongsTo(EmployeeDetailModel::class, 'employee_id');
    }
}
